﻿var forum = {};
forum.baseURL = 'http://localhost:8080/website';
//不允许放入frame中
if(top!=self){
	//除了登录页面和注册页面
	if(self.location.href != forum.baseURL + '/login?' && 
			self.location.href != forum.baseURL + '/signup?'
	){
		top.location=self.location;		
	}
}

forum.op = {
	
	deleteSubject : function(){
		var ids = '';
		$('input[name=selectTopic]:checked').each(function(){
			ids = ids + $(this).val() + '_';
		});
		$.post(forum.baseURL + "/topic/" + ids, {_method:'delete'}, function(json){
			if(json.success){
				location.href=location.href;
			} else {
				alert(json);
			}
		},'json');
	},

	digestSubject : function(){
		var ids = '';
		$('input[name=selectTopic]:checked').each(function(){
			ids = ids + $(this).val() + '_';
		});
		$.post(forum.baseURL+"/topic/" + ids + '/set-digest', {_method:'put'}, function(json){
			if(json.success){
				location.href=location.href;
			} else {
				alert(json);
			}
		},"json");
	},

	topSubject : function(){
		var ids = '';
		$('input[name=selectTopic]:checked').each(function(){
			ids = ids + $(this).val() + '_';
		});
		$.post(forum.baseURL+"/topic/" + ids + "/set-top", {_method:'put'}, function(json){
			if(json.success){
				location.href=location.href;
			} else {
				alert(json);
			}
		},"json");
	},
	
	moveSubject : function(id,elem){
		var selectElem = $('select.admin-select-category:last');
		if(selectElem.val()==0){	//栏目选择菜单已经给出, 但没有选择栏目
			selectElem.remove();
			return;
		}else if(selectElem.val()>0){	//已经选择了栏目
			var ids = '';
			$('input[name=selectTopic]:checked').each(function(){
				ids = ids + $(this).val() + '_';
			});
			$.post(forum.baseURL+"/topic/" + ids + "/move", {categoryId:selectElem.val(),_method:'put'}, function(json){
				if(json.success){
					alert("已经转移");
					location.href=location.href;
				} else {
					alert(json);
				}
			},"json");
			return;
		}
		//给出栏目选择菜单
		$.getJSON(forum.baseURL+"/category/"+id+"/children", function(data){
			if(data.returnObject == null || data.returnObject.length==0){
				return;
			}
			var html='<select class="admin-select-category">' + '<option value="0">...</option>';
			for(i=0;i<data.returnObject.length;i++){
				option = '<option value="' + data.returnObject[i].id + '">' +
						data.returnObject[i].name + '</option>';
				html = html + option;
			}
			html = html + '</select>';
			$(elem).after(html);
			//栏目联级选择
			$('select.admin-select-category').live('change',function(){
				var index = $('select.admin-select-category').index(this);
				$('select.admin-select-category:gt('+index+')').remove();
				var id = $(this).val();
				if(id == 0){
					return;
				}
				$.getJSON(forum.baseURL+"/category/"+id+"/children", function(data){
					if(data.returnObject == null || data.returnObject.length==0){
						return;
					}
					var html='<select class="admin-select-category">' + '<option value="0">...</option>';
					for(i=0;i<data.returnObject.length;i++){
						option = '<option value="' + data.returnObject[i].id + '">' +
								data.returnObject[i].name + '</option>';
						html = html + option;
					}
					html = html + '</select>';
					$('select.admin-select-category:last').after(html);
				});
			});
		});
	},
	
	rollTopRank : function(id){
		$.post(forum.baseURL+"/topic/" + id + "/roll-top-rank", {_method:'put'}, function(json){
			if(json.success){
				location.href=location.href;
			} else {
				alert(json);
			}
		},"json");
	},

	deleteReply : function(id){
		$.post(forum.baseURL + "/reply/" + id, {_method:'delete'}, function(json){
			if(json.success){
				alert("删除成功");
				location.href=location.href;
			}
		},"json");
	},
	
	hideReply : function(id, bool){
		$.post(forum.baseURL + "/reply/"+id+"/hide?_method=PUT", {}, function(json){
			if(json.success){
				if(bool){
					alert("已经隐藏");
					location.href=location.href;
				} else {
					alert("已经显示");
					location.href=location.href;
				}
			}
		},"json");
	},

	hideSubject : function(id, bool){
		$.post(forum.baseURL + "/subject/"+id+"/hide?_method=PUT", {}, function(json){
			if(json.success){
				if(bool){
					alert("已经隐藏");
					location.href=location.href;
				} else {
					alert("已经显示");
					location.href=location.href;
				}
			}
		},"json");
	},
	
	forbidReply : function(subjectId, bool){
		$.post(forum.baseURL + "/forum/subject/" + subjectId, {forbidReply:bool,_method:'put'}, function(json){
			if(json.process == "success"){
				if(bool){
					alert("该主题已禁止回复");
					location.href=location.href;
				} else {
					alert("该主题已允许回复");
					location.href=location.href;
				}
			}
		},"json");
	},
	
	register : function(){
		$("input[name='submit']").attr("disabled", "disabled");
		
		var nickname = $("input[name='nickname']").val();
		var password = $("input[name='password']").val();
		var _password = $("input[name='_password']").val();
		var sex = $("input[name='sex']").val();
		var email = $("input[name='email']").val();
		var captcha = $("input[name='captcha']").val();

		$(".error").text("");
		
		if(password != _password){
			$("input[name='_password']").next().text("两次密码不一致");
			$("input[name='submit']").removeAttr("disabled");
			return;
		}
		var data = {nickname:nickname,password:password,sex:sex,email:email,captcha:captcha};
		$.post(forum.baseURL + "/register", data, function(json){
				if(json.process == "success"){
					self.parent.forum.dialog.registerSuccess();
				} else {
					if(json.nickname != "pass"){
						$("input[name='nickname']").next().text(json.nickname);
					}
					if(json.password != "pass"){
						$("input[name='password']").next().text(json.password);
					}
					if(json.sex != "pass"){
						$("input[name='sex']").next().text(json.sex);
					}
					if(json.email != "pass"){
						$("input[name='email']").next().text(json.email);
					}
					if(typeof(json.captcha) != 'undefined' && json.captcha != "pass"){
						$("input[name='captcha']").next().text(json.captcha);
						$("input[name='captcha']").val('');
						$("#captcha").attr("src", "captcha?r=" + Math.random());
					}
				}
				$("input[name='submit']").removeAttr("disabled");
			}, "json");
	},
	
	login : function(form){
		$("input[name='submit']").attr("disabled", "disabled");
		
		var nickname = $("input[name='nickname']").val();
		var password = $("input[name='password']").val();
		if($("input[name='remember']:checked").val() != undefined){
			var remember = $("input[name='remember']").val();
		}

		$(".error").text("");
		var data = form.serializeObject();
		$.postJson(forum.baseURL + "/login.json", data, function(json){
				if(json.process == "success"){
					self.parent.forum.dialog.loginSuccess();
				} else {
					if(json.nickname != "pass"){
						$("input[name='nickname']").next().text(json.nickname);
					}
					if(json.password != "pass"){
						$("input[name='password']").next().text(json.password);
					}
				}
				$("input[name='submit']").removeAttr("disabled");
			}, "json");
	},
	
	updateAccountInfo : function(id){
		$("input[type='submit']").attr("disabled", "disabled");
		$("span.error").text('');
		var sign = $("input[name=sign]").val();
		var intro = KE.util.getData("intro");
		var factory = $("input[name='factory']").val();
		var birthday = $("input[name=birthday]").val();
		var qq = $("input[name=QQ]").val();
		var sex = $("select[name=sex]").val();
		var province = $("select[name=province]").val();
		var city = $("select[name=city]").val();
		var residence = $("select[name=residence]").val();

		var data = {sign : sign, intro : intro, factory:factory, birthday:birthday, sex : sex, qq:qq, id: id, type:1};

		if(province != undefined && city != undefined){
			data.province = province;
			data.city = city;
		}
		if(residence != undefined){
			data.residence = residence;
		}
		$.postJSON(forum.baseURL + "/account/" + id+"?_method=PUT", data, function(json){
			if(json.success){
				alert("修改成功");
			}else{
				printFormError(json);
			}
			$("input[type='submit']").removeAttr("disabled");
		});
	},
	
	sendMessage : function(url){
		var receiverName = $('input[name=receiverName]').val();
		if(receiverName.length == 0){
			alert("请填写接收者的昵称");
			return;
		}
		var content = KE.util.getData("content");
		if(content.length == 0){
			alert("请输入短消息内容");
			return;
		}
		var data = {receiverName:receiverName, content:content};
		$.post(forum.baseURL + "/message", data, function(json){
			if(json.success){
				alert("发送成功");
				if(url == undefined){
					location.href=location.href;
				}else{
					location.href=url;
				}
			} else{
				alert(json.message)
			}
		},'json');
	},
	
	deleteMessage : function(){
		var checked = $("input[type=checkbox]:checked");
		if(checked.val() != undefined){
			var ids = new Array();
			checked.each(function(){
				ids.push($(this).val());
			});
		} else{
			alert("请选择要删除的短消息");
			return;
		}
		var data = {ids:ids, _method:"delete"};
		$.post(forum.baseURL + "/message/delete", data, function(json){
			if(json.process == "success"){
				location.href=location.href;
			} else{
				alert(json.message);
			}
		}, "json");
	},
	
	deleteAllInBox : function(box){
		var url = forum.baseURL + "/message/box";
		var data = {_method:'delete'};
		var regexS = "[\\?&]type=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results != null && results[1] == 'sent')
			data.type = 'sent';
		
		$.post(url, data, function(json){
			if(json.process == "success"){
				alert("已经清空");
				location.href=location.href;
			} else{
				alert(json.message)
			}
		}, "json");
	},
	
	deleteFavorite: function(){
		var checked = $("input[type=checkbox]:checked");
		var ids = new Array();
		if(checked.val() != undefined){
			checked.each(function(){
				ids.push($(this).val());
			});
		} 
		if(ids.length == 0){
			alert("请选择要删除的收藏");
			return;
		}
		var data = {ids:ids, _method:"delete"};
		$.post(forum.baseURL + "/favorite/delete", data, function(json){
			if(json.process == "success"){
				location.href=location.href;
			} else{
				alert(json.message);
			}
		}, "json");
	}
	
};

forum.dialog = {
		registerSuccess : function(){
			tb_remove();
			setTimeout(function(){
				var frameCount = window.frames.length;
				$("#login_dialog").click();
				while(window.frames.length - frameCount == 0){
				}
				frameCount = window.frames.length;
				$(window.frames[frameCount-1].document).ready(function(){
					setTimeout(function(){
						$(window.frames[frameCount-1].document).find("#title").text("注册成功，请登录");
					},200);
				});
			},400);
		},
		
		loginSuccess : function(){
			tb_remove();
			location.href=location.href;
		},
		
		toLoginForm : function(){
			tb_remove();
			setTimeout(function(){
				$("#register_dialog").click();
			},400);
		},
		
		//参数：content, closeButton, width, height
		message : function(config){
			$('#dialog').remove();
			var dialogHtml = '<div id="dialog" style="display:none">';
			if(config.closeButton){
				dialogHtml = dialogHtml + '<span title="关闭" style="position:absolute;top:2px;right:2px;color:#6BC870;font-size:16px; font-weight:bold; cursor:pointer;" onclick="tb_remove();">×</span>';
			}
			dialogHtml = dialogHtml + '<h3 style="font-size:16px;font-weight:bold;">提示信息：</h3>'
									+ '<p style="text-indent:2.6em;font-size:14px">'
									+ config.content
									+ '</p>'
									+ '</div>';
			$('body').append(dialogHtml);
			var url = '#TB_inline?height=' + config.height + '&amp;width=' + config.width + '&amp;inlineId=dialog&amp;modal=true';
			tb_show(null, url ,false);
			$('#TB_overlay').css('filter', 'alpha(opacity=0)');
			$('#TB_overlay').css('-moz-opacity', '0');
			$('#TB_overlay').css('opacity', '0');
			if(!config.closeButton){
				setTimeout(function(){
					tb_remove();
					},1400);
			}
		}
/*		
		form: function(title, content, ){
			$('#dialog').remove();
			var dialogHtml = '<div id="dialog" style="display:none">';
			dialogHtml = dialogHtml + '<span title="关闭" style="position:absolute;top:2px;right:2px;color:#6BC870;font-size:16px; font-weight:bold; cursor:pointer;" onclick="tb_remove();">×</span>';
			dialogHtml = dialogHtml + '<h3 style="font-size:16px;font-weight:bold;">'+ title + '</h3>'
			+ '<div>'
			+ content
			+ '</div>'
			+ '<div><button onclick="$(#dailog form).submit()">确认</button><button>取消</button></div>'
			+ '</div>';
		}*/
};

forum.favoritePlug = function(e){
	favoriteImage1 = new Image();// preload image
	favoriteImage2 = new Image();// preload image
	favoriteImage3 = new Image();// preload image
	favoriteImage1.src = forum.baseURL + '/static/favorite1.gif';
	favoriteImage2.src = forum.baseURL + '/static/favorite2.gif';
	favoriteImage3.src = forum.baseURL + '/static/favorite3.gif';
	e.prepend('<img title="收藏" alt="收藏" src="' + favoriteImage1.src + '"/>');
	var favorite = $('span.floor img');
	var disableFavorite = function(f){
		f.unbind('mouseover');
		f.unbind('mouseout');
		f.unbind('click');
		f.attr('src', favoriteImage3.src);
	};
	
	favorite.css('cursor','pointer')
			.css('margin-right', '5px')
			.css('vertical-align','middle')
			.bind('mouseover', function(e){
				$(this).attr('src', favoriteImage2.src);
			}).bind('mouseout', function(e){
				$(this).attr('src', favoriteImage1.src);
			}).bind('click', function(e){
				var anchorName = $(this).next().next().attr('name');
				var index = anchorName.indexOf("subject");
				var type = '';
				var id = 0;
				if(index != -1){
					type = 'topic';
					id = anchorName.substring(7);
				}else {
					index = anchorName.indexOf("reply");
					if(index != -1){
						type = 'reply';
						id = anchorName.substring(5);
					}else {
						alert("出现错误,可以刷新一下网页再试试");
					}
				}
				var t = $(this);
				$.post(forum.baseURL+'/favorite/'+type+'/'+id, {}, function(json){
					if(json.success){
						disableFavorite(t);
						forum.dialog.message({content:'收藏成功', closeButton:false, width:200, height:100});
					} else{
						forum.dialog.message({content:json.message, closeButton:false, width:200, height:100});
					}
					}, 'json');
			});
};

forum.showFloorURL = function(){
	var floor = $('span.floor i');
	floor.bind('mouseover',function(e){
		$(this).css('color','red');
	});
	floor.bind('mouseout',function(e){
		$(this).css('color','black');
	});
	floor.bind('click',function(e){
		var url;
		anchorName = $(this).next().attr('name');
		index = anchorName.indexOf("subject");
		if(index != -1){
			url = location.href;
		}else {
			index = anchorName.indexOf("reply");
			if(index != -1){
				replyId = anchorName.substring(5);
				url = forum.baseURL + '/forum/reply/' + replyId;
			}
		}
		forum.dialog.message({content:'地址：<a href="' + url + '" style="color:green;display:inline;">' + url + '</a>', closeButton:true, width:400, height:100});
	});
};

forum.insertIntoEditor = function(floor, replyId){
	editorId = 'topic_content';
	KE.util.focus(editorId);
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 5000}, 1100);
	if(floor == undefined){
		return;
	}
	var html = '<span class="replyTitle">回复:<a href="' + forum.baseURL + '/forum/reply/' + replyId + '">' + floor + '</a></span><br/>';
	data = KE.util.getData(editorId);
	KE.util.selection(editorId);
	if(data == ''){
		KE.util.insertHtml(editorId, html);
	} else {
		KE.util.insertHtml(editorId, '<br/><br/>' + html);
	}
};

forum.showUserInfo = function(){
	
};

forum.broadcast = function(){
	var list=$('#header .broadcast span')
	var r = Math.floor(Math.random()*list.length);
	$(list[r]).show();
	var times = r+1;
	setInterval(function(){
		i = times % list.length;
		list.hide();
		$(list[i]).show();
		++ times;
	}, 8000);
};

forum.stat={};
forum.stat.params={};
forum.stat.initParams=function(id){
	p=forum.stat.params;
	p.id=id;
	p.color=navigator.appName=="Netscape"?pc=screen.pixelDepth:pc=screen.colorDepth;
	p.screenWidth=screen.width;
	p.screenHeight=screen.height;
	p.clientWidth=document.documentElement.clientWidth;
	p.clientHeight=document.documentElement.clientHeight;
	p.timeZone=0-new Date().getTimezoneOffset()/60;
};
forum.stat.sendData=function(){
	params=forum.stat.params;
	if(params.id==undefined || params.id==0){
		return;
	}
	url=forum.baseURL+'/stat.servlet?';
	for(param in params){
		url=url+param+'='+params[param]+'&';
	}
	img=new Image();
	img.src=url;
};
window.onbeforeunload=forum.stat.sendData;

forum.thumbImg =  function(obj, method) {
	var imagemaxwidth = '600';
	
	if(!obj) {return;}
	obj.onload = null;
	file = obj.src;
	zw = obj.offsetWidth;
	zh = obj.offsetHeight;
	if(zw < 2) {
		if(!obj.id) {
			obj.id = 'img_' + Math.random();
		}
		setTimeout("forum.thumbImg($('#" + obj.id + "')[0], " + method + ")", 100);
		return;
	}
	zr = zw / zh;
	method = !method ? 0 : 1;
	if(method) {
		fixw = obj.getAttribute('_width');
		fixh = obj.getAttribute('_height');
		if(zw > fixw) {
			zw = fixw;zh = zw / zr;
		}
		if(zh > fixh) {
			zh = fixh;zw = zh * zr;
		}
	} else {
		var widthary = imagemaxwidth.split('%');
		if(widthary.length > 1) {
			fixw = $('#main .topic .content_wrap .content')[0].clientWidth - 200;
			if(widthary[0]) {
				fixw = fixw * widthary[0] / 100;
			} else if(widthary[1]) {
				fixw = fixw < widthary[1] ? fixw : widthary[1];
			}
		} else {
			fixw = widthary[0];
		}
		if(zw > fixw) {
			zw = fixw;zh = zw / zr;obj.style.cursor = 'pointer';
			if(!obj.onclick) {
				obj.onclick = function() {
					window.open(obj.src, '', '');
					};
			}
		}
	}
	obj.width = zw;obj.height = zh;
}


$(document).ready(function(){
	$('body').ajaxError(function(event, response, settings, exception) {
		if(response.status == 404){
			alert("找不到您请求的地址");
		} else if(response.status == 403){
			if(settings.url==forum.baseURL + '/favorite'){
				forum.dialog.message({closeButton:false, width:300, height:100, content:'登录以后才能使用收藏功能'});
			}else {
				alert("您没有权限进行此操作");	
			}	
		} else if(response.status == 500){
			alert("网站出现错误");
		}
	});
	
	//栏目联级选择
	$('select.select-category').live('change',function(){
		index = $('select.select-category').index(this);
		$('select.select-category:gt('+index+')').remove();
		var id = $(this).val();
		if(id == 0){
			//do something
			return;
		}
		$("input[type='submit']").attr("disabled", "disabled");
		$.getJSON(forum.baseURL+"/category/"+id+"/children", function(data){
			if(data.returnObject == null || data.returnObject.length==0){
				$("input[type='submit']").removeAttr("disabled");
				return;
			}
			var html='<select class="select-category">' + '<option value="0">...</option>';
			for(i=0;i<data.returnObject.length;i++){
				option = '<option value="' + data.returnObject[i].id + '">' +
						data.returnObject[i].name + '</option>';
				html = html + option;
			}
			html = html + '</select>';
			$('select.select-category:last').after(html);
			$("input[type='submit']").removeAttr("disabled");
		});
	});

	//发表回复
	$('#reply-from').submit(function(){
		$("input[type='submit']").attr("disabled", "disabled");
		var action = $(this).attr('action');
		$('span.error').remove();
		var content = editor.getSource();
		var data = {'content':content};
		$.postJSON(action, data, function(json){
			if(json.success){
				content = content.replace(new RegExp('event="thumbImg"','gm'),'onload="forum.thumbImg(this)"');
				photoPath = forum.baseURL + '/static/photo.jpg';
				if(json.returnObject.reply.author.account.extInfo.photoPath){
					photoPath = forum.baseURL + json.returnObject.reply.author.account.extInfo.photoPath;
				}
				html = '<div class="content_wrap">' + '<i class="floor">' + 
						json.returnObject.floor + '楼</i><div class="content">' + 
						json.returnObject.reply.content + '</div></div><div class="info"><a href="' + 
						forum.baseURL + '/member/' + json.returnObject.reply.author.id + 
						'"><img src="' + photoPath + '"/></a><span class="nickname">' + 
						json.returnObject.reply.author.account.nickname + 
						'</span></div><div class="clear"></div><div class="time">' + json.returnObject.reply.date + 
						'</div>';
				
				$("div[class=time]:last-child").after(html);
				forum.dialog.message({content:'发表成功',close:false,width:200,height:80});
				//清空编辑器
				tinyMCE.activeEditor.setContent();
			} else {
				for(i=0;i<json.errors.length;i++){
					$("input[type='submit']").after('<span class="error" style="color:red">*'+ json.errors[i].message + '</span>');
				}
			}
			$("input[type='submit']").removeAttr("disabled");
		});
		return false;
	});
	
	//发表主题
	$('#subjectForm').submit(function (){
		$("input[type='submit']").attr("disabled", "disabled");
		$("span.error").remove();
		var title = $.trim($("input[name=title]").val());
		if(title.length == 0){
			$("input[name='title']").after('<span class="error" style="color:red;">*标题必须填写</span>');
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var content = editor.getSource();
		
		if($.trim(content).length < 10){
			$("input[name='submit']").after('<span class="error" style="color:red;">*内容太短了,不能发表</span>');
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var categoryId = $('select.select-category:last').val();
		if(categoryId == 0){
			alert("请选择栏目");
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var data = {title:title, content:content, categoryId:categoryId};
		$.postJSON($(this).attr('action'), data, function(json){
			if(json.success){
				location.href=json.returnObject;
			} else {
				$("input[type='submit']").after('<span class="error" style="color:red;">*' + json.message + '</span>');
				$("input[type='submit']").removeAttr("disabled");
			}
		});
		return false;
	});
	
	
	
	
	//编辑主题
	$('#editSubjectForm').submit(function(){
		$("input[type='submit']").attr("disabled", "disabled");
		$("span.error").remove();
		var title = $.trim($("input[name=title]").val());
		if(title.length == 0){
			$("input[name='title']").after('<span class="error" style="color:red;">*标题必须填写</span>');
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var content = KE.util.getData("topic_content");
		if($.trim(content).length < 10){
			$("input[type='submit']").after('<span class="error" style="color:red;">*内容太短了,不能发表</span>');
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var data = {title:title, content:content};
		$.postJSON($(this).attr('action')+'?_method=PUT', data, function(json){
			if(json.success){
				location.href=json.returnObject;
			} else {
				$("input[type='submit']").after('<span class="error" style="color:red;">*' + json.message + '</span>');
				$("input[type='submit']").removeAttr("disabled");
			}
		});
		return false;
	});
	
	//编辑回复
	$('#editReplyForm').submit(function(){
		$("input[type='submit']").attr("disabled", "disabled");
		$("span.error").remove();
		var content = KE.util.getData("topic_content");
		if($.trim(content).length < 10){
			$("input[type='submit']").after('<span class="error" style="color:red;">*内容太短了,不能发表</span>');
			$("input[type='submit']").removeAttr("disabled");
			return false;
		}
		var data = {content:content};
		$.postJSON($(this).attr('action')+'?_method=PUT', data, function(json){
			if(json.success){
				location.href=json.returnObject;
			} else {
				$("input[type='submit']").after('<span class="error" style="color:red;">*' + json.message + '</span>');
				$("input[type='submit']").removeAttr("disabled");
			}
		});
		return false;
	});

	//登录
	$('.login').submit(function(){
		var form = $(this).serializeObject();
		$.postJSON($(this).attr("action"), form, function(data){
			if(data.success){
				self.parent.forum.dialog.loginSuccess();
			} else {
				for(i=0;i<data.errors.length;i++){
					if(data.errors[i].element==null){
						alert(data.errors[i].message);
					}else {
						$("*[name='"+data.errors[i].element+"']").next().text(data.errors[i].message);
					}
				}
			}
		});
		return false;
	});
});

function printFormError(json){
	if(json.success){
		return;
	}
	for(var i=0;i<json.errors.length;i++){
		if(json.errors[i].element==null){
			alert(json.errors[i].message);
		}else {
			$("*[name='"+json.errors[i].element+"']").next().text(json.errors[i].message);
		}
	}
}
